package com.ybkj.o2o.zhuanxian.manager.impl;

import com.ybkj.o2o.zhuanxian.entity.Orbit;
import com.ybkj.o2o.zhuanxian.manager.OrderOrbitManager;
import com.ybkj.o2o.zhuanxian.service.OrderOrbitService;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DefaultOrderOrbitManager implements OrderOrbitManager {
  
  @Autowired
  private OrderOrbitService orderOrbitService;

  @Override
  public Orbit findByOrderId(Long orderId) {

    return orderOrbitService.findByOrderId(orderId);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void insert(Long orderId, String xml) {

    Document doc = null;

    try {

      doc = DocumentHelper.parseText(xml);
      Element root = doc.getRootElement();

      Element element = root.element("order");

      // 解析xml节点
      Orbit orderorbit = new Orbit();
      orderorbit.setOrderId(orderId);
      orderorbit.setXml(element.element("locations").asXML());
      orderOrbitService.insert(orderorbit);
    } catch (DocumentException ex) {
      ex.printStackTrace();
    }


  }
}
